<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\admin\model\Job;
use app\admin\model\User;
use think\Request;

class Phone extends Rbac
{
//    电话部页面
    public function phone()
    {
        $power = $_SESSION['u_r_id'];

        $user_name = $_SESSION['u_name'];
        $where = [
           'job_getman'=>$user_name
        ];
        $where = ($power==3 or $power==1)?null:$where;
        $startTime = date('Y-m-01', time());
        $endTime = date('Y-m-d', strtotime("$startTime +1 month"));
//        echo $endTime;exit;
        $res = $this->screen($startTime,$endTime,$where);
        $info = $this->screen_list($startTime,$endTime,$where);
        $this->assign('result',$info);
        $this->assign('res',$res);
        return $this->fetch('phone/phone');
    }
//    查看全部
    public function all_search(){
        $where = "job_getman!=''";
        $startTime = date('Y-m-01', time());
        $endTime = date('Y-m-d', strtotime("$startTime +1 month"));
//        echo $endTime;exit;
        $res = $this->screen($startTime,$endTime);
        $info = $this->screen_list($startTime,$endTime,$where);
        $this->assign('result',$info);
        $this->assign('res',$res);
        return $this->fetch('phone/phone');
    }

//    电话部接通状态搜索
    public function search_sn(Request $req){
        $job_phone_sn = $req->post('sn');
        $power = $_SESSION['u_r_id'];

        $user_name = $_SESSION['u_name'];
        $where = [
            'job_getman'=>$user_name
        ];
        $where = ($power==3 or $power==1)?null:$where;
        $info = Job::where('job_phone_sn like \'%'.$job_phone_sn.'%\'')
            ->where($where)
            ->select();
        $count = count($info);
        $info = Job::where('job_phone_sn like \'%'.$job_phone_sn.'%\'')
            ->where($where)
            ->paginate(10,$count);
        return json_encode($info);
    }
    //    查看详情页面
    public function test(){
        $job_id = request()->param('job_id');
        $result = Job::get($job_id)->toArray();
        $this->assign('result',$result);
        return $this->fetch('phone/test');
    }
//    电话部电话搜索
    public function search(Request $req){
        $job_phone = $req->post('phone');
        $power = $_SESSION['u_r_id'];

        $user_name = $_SESSION['u_name'];
        $where = [
            'job_getman'=>$user_name
        ];
        $where = ($power==3 or $power==1)?null:$where;
        $info = Job::where('job_phone like \'%'.$job_phone.'%\'')
            ->where($where)
            ->select();
        $count = count($info);
        $info = Job::where('job_phone like \'%'.$job_phone.'%\'')
            ->where($where)
            ->paginate(10,$count);
        return json_encode($info);
    }
//    电话部时间筛选
    public function time_search(Request $req){
            $where = '';
            $startTime = $req->param('start');
            $endTime = $req->param('end');
            if(empty($startTime)) {
                $startTime = date('Y-m-01', time());
                $endTime = date('Y-m-d', strtotime("$startTime +1 month"));
            }
            $endTime = date('Y-m-d', strtotime("$endTime +1 day"));
            $res = $this->screen($startTime, $endTime);
            $info = $this->screen_list($startTime, $endTime, $where);
            $this->assign('result', $info);
            $this->assign('res', $res);
        return $this->fetch('phone/phone');
    }
//    电话部月份筛选
    public function month_search(Request $req){
        $val = $req->param('month');
        $val = empty($val)?date('m',time()):$val;
        echo $val;
        $where = '';
        $nowmonth = date("Y",time()).'-'.$val.'-1';
        $v = ($val+1)>12?1:$val+1;
        $nextmonth = date("Y",time()).'-'.$v.'-1';
//        echo $nowmonth,'--',$nextmonth;
        $res = $this->screen($nowmonth,$nextmonth);
        $info = $this->screen_list($nowmonth,$nextmonth,$where);
        $this->assign('result',$info);
        $this->assign('res',$res);
        return $this->fetch('phone/phone');
    }
    private function screen_list($startTime,$endTime,$where=null){
        $startTime = strtotime($startTime);
        $endTime = strtotime($endTime);
        $info = Job::where($where)
            ->whereTime('job_createtime','between',[$startTime,$endTime])
            ->select();
//        var_dump(Db::table('mdm_user')->getLastSql());exit;
        $count = count($info);
        $info = Job::where($where)
            ->whereTime('job_createtime','between',[$startTime,$endTime])
            ->paginate(10,$count);
        return $info;
    }
//    筛选上门人数等数据
    private function screen($startTime,$endTime,$where=null){
        $startTime = strtotime($startTime);
        $endTime = strtotime($endTime);
        //      电话师个人上门量
        $inter_join = 'u.user_name=j.job_getman and job_get_time between '.$startTime.' and '.$endTime.' ';
        $inter_num = Db::table('mdm_user')->alias('u')
            ->field('u.user_name,COUNT(j.job_id) as num')
            ->join('mdm_job j',$inter_join,'left')
            ->where('user_r_id in(3,4)')
            ->where($where)
            ->group('u.user_name')
            ->select();

//     电话师个人成交人数
        $deal_join = 'u.user_name=j.job_getman and job_order_time between '.$startTime.' and '.$endTime.' ';
        $deal_num = Db::table('mdm_user')->alias('u')
            ->field('u.user_name,COUNT(j.job_id) as num')
            ->join('mdm_job j',$deal_join,'left')
            ->where('user_r_id in(3,4)')
            ->group('u.user_name')
            ->select();
//      电话师个人有效人数
        $valid_join = 'u.user_name=j.job_getman and job_createtime between '.$startTime.' and '.$endTime.' and job_valid_sn=1';
        $valid_num = Db::table('mdm_user')->alias('u')
            ->field('u.user_name,COUNT(j.job_id) as num')
            ->join('mdm_job j',$valid_join,'left')
            ->where('user_r_id in(3,4)')
            ->group('u.user_name')
            ->select();
//   电话师个人无效人数
        $invalid_join = 'u.user_name=j.job_getman and job_createtime between '.$startTime.' and '.$endTime.' and job_valid_sn=2';
        $invalid_num = Db::table('mdm_user')->alias('u')
            ->field('u.user_name,COUNT(j.job_id) as num')
            ->join('mdm_job j',$invalid_join,'left')
            ->where('user_r_id in(3,4)')
            ->group('u.user_name')
            ->select();
//      将拿到的数据组装成数组
        $res = [];
        for($i = 0;$i < count($inter_num);$i++){
//            $user_name = $inter_num[$i]['user_name'];
            $res[$i][] = $inter_num[$i];
            $res[$i][] = $deal_num[$i];
            $res[$i][] = $valid_num[$i];
            $res[$i][] = $invalid_num[$i];
        }
        return $res;
    }
//    电话部分配人员
    public function allot(Request $req){
        if($req->post()){
            $user_name = $req->post('user_name');
            $job_id = $_SESSION['job_id'];
            $data = [
                'job_getman'=>$user_name
            ];
            $res = Job::where('job_id in('.$job_id.')')->update($data);
            if($res>=0){
                echo "<script>parent.layer.msg('分配成功',{icon:1,time:2000});var index = parent.layer.getFrameIndex(window.name);parent.layer.close(index); setTimeout(function () {main.location.href='admin/Phone/phone';
window.parent.location.reload();},2000);</script>";
            }else{
                $this->error('分配失败！');
            }
            unset($_SESSION['job_id']);
        }
        $_SESSION['job_id'] = $req->param('job_id');
        $info = User::where('`user_r_id`=3 or user_r_id=4')
            ->field('user_id,user_name')
            ->select();
        $this->assign('res',$info);
        return $this->fetch('phone/allot');
    }
//     电话部修改
    public function phone_update(Request $req){
        if($req->post()){
            $data = input('post.');
            $job_id = $data['job_id'];
            $data['job_jointime'] = strtotime($data['job_jointime']);
            unset($data['job_id']);
            $res = Job::where('job_id='.$job_id)
                ->update($data);
            if($res>0){
                $this->success('修改成功！','admin/phone/phone');
            }else{
                $this->error('修改失败！','admin/phone/phone');
            }
        }
        $info = Job::get($req->param('job_id'));
        $this->assign('res',$info);
        return $this->fetch('phone/phoneupdate');
    }
    //   修改有效无效状态
    public function isCome(Request $req){
        $job_valid_sn = $req->post('job_get_sn');
        $job_id = $req->post('job_id');
        $res = Job::where('job_id='.$job_id)->update([
            'job_valid_sn'=>$job_valid_sn
        ]);
        if($res>=0){
            return 200;
        }else{
            return 100;
        }

    }
    //    电话部人员管理
    public function person()
    {
        $info = User::all();
        $count = count($info);
        $info = User::where('`user_r_id`=3 or user_r_id=4')->paginate(10,$count);
        $this->assign('result',$info);
        return $this->fetch('phone/person');
    }
//    电话部人员添加
    public function add(Request $req){
        if($req->post()){
            $data = input('post.');
            $data['user_r_id'] = 4;
            $res = User::insert($data);
            if($res>0){
                $this->success('添加成功！','admin/phone/person');
            }else{
                $this->error('添加失败！','admin/phone/person');
            }
        }
        return $this->fetch('phone/personadd');
    }
//    电话部人员修改
    public function person_update(Request $req){
        if($req->post()){
            $data = input('post.');
            $user_id = $data['user_id'];
            unset($data['user_id']);
            $res = User::where('user_id='.$user_id)
            ->update($data);
            if($res>0){
                $this->success('修改成功！','admin/phone/person');
            }else{
                $this->error('修改失败！','admin/phone/person');
            }
        }
        $user_id = $req->param('user_id');
//        var_dump($user_id);exit;
        $info = User::get($user_id)->toArray();
        $this->assign('res',$info);
        return $this->fetch('phone/personupdate');
    }
//    电话人员删除
    public function delete(Request $req){
        $user_id = $req->param('user_id');
        $res = User::destroy($user_id);
        if($res>0){
            return 200;
        }else{
            return 100;
        }
    }
}
